package com.fhd.filter;

import com.fhd.constant.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SystemFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        Object client = req.getSession().getAttribute(Constants.CLIENT_SESSION);
        Object merchant = req.getSession().getAttribute(Constants.MERCHANT_SESSION);
        Object manager = req.getSession().getAttribute(Constants.MANAGER_SESSION);

        if (client == null && merchant == null && manager == null) {
            // 没有登录，转到错误页面去
            req.getRequestDispatcher("../error.jsp").forward(req,resp);
        }
        filterChain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
